package com.listeningframework.boot.codegen.generators.coders;

import com.google.inject.internal.util.Lists;
import com.listeningframework.boot.codegen.constants.Templates;
import com.listeningframework.boot.codegen.context.EntityContext;
import com.listeningframework.boot.codegen.context.TableMetaData;
import com.listeningframework.boot.codegen.exception.CodeGenException;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author lixining
 * @version $Id: EntityCoder.java, v 0.1 2016年12月5日 下午1:53:23 lixining Exp $
 */
public class EntityCoder extends AbstractCoder {
    /**
     * 父节点基本属性
     */
    private static final String[] PARENT_COLUMNS = {"id", "created_by", "created_date", "updated_by", "updated_date"};

    public EntityCoder(EntityContext entityContext) {
        super(entityContext);
    }

    @Override
    protected String getContent(Templates template) throws CodeGenException {
        return render(template);
    }

    @Override
    protected Map<String, Object> getParams(Templates templates) {
        Map<String, Object> paramMap = newHashMap(templates);
        paramMap.put(CLASS_IMPORTS, entityContext.getEntityImports());
        paramMap.put(CLASS_METADATA_LIST, getTableMetaDataList());
        return paramMap;
    }

    private List<TableMetaData> getTableMetaDataList() {
        if (entityContext.isWithCreateInfo()) {
            return entityContext.getTableMetaDataList();
        }
        List<TableMetaData> result = Lists.newArrayList();
        for (TableMetaData tableMetaData : entityContext.getTableMetaDataList()) {
            if (!Arrays.asList(PARENT_COLUMNS).contains(StringUtils.lowerCase(tableMetaData.getColumnName()))) {
                result.add(tableMetaData);
            }
        }
        return result;
    }

}
